<%@page import="net.groupme.user.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.Group"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="java.util.Collections"%>
<%@page import="net.groupme.model.dao.DATABASE_CONSTANTS"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.*"%>
<%@page import="net.groupme.model.Tag"%>
<div class="grouplistHead" style="margin-top:1em;">
	<h4 style="float: left;">Your Groups 
		<span style="font-size: x-small; color: gray; ">
		<% 	String orderedBy = "";
		List<Group> mygroups = null;
		int numberofGroups = 0;

		//ListIterator<Group> it = groupsOfUser.listIterator(count);
		UserManager um = (UserManager) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
		.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class);
		if (VIEW_VOCABULARY.SORT_ALPHA_ASC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.RESOURCE_NAME);
			orderedBy = "in alphabetical order (a..z)";
		}
		else if (VIEW_VOCABULARY.SORT_ALPHA_DESC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.RESOURCE_NAME);
			Collections.reverse(mygroups);
			orderedBy = "in alphabetical order (z..a)";
		}
		else if (VIEW_VOCABULARY.SORT_TAD_ASC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.TABLE_USERGROUP + "." + DATABASE_CONSTANTS.USER_GROUP_TAD);
			orderedBy = "in temporal order (oldest first)";
		}
		else {
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.TABLE_USERGROUP + "." + DATABASE_CONSTANTS.USER_GROUP_TAD);
			Collections.reverse(mygroups);
			orderedBy = "in temporal order (newest first)";
		}
			%>
			<%= orderedBy %>
		</span> 
	</h4>
	<div class="groupListHeadMenu" style="text-align: right;">
		<form>
		<small>sort by: </small>
			<select onchange="location.href=this.options[this.selectedIndex].value;">
				<option>please select</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_ALPHA_ASC %>">alphabet (a..z)</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_ALPHA_DESC %>">alphabet (z..a)</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_TAD_DESC %>">date (newest first)</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_TAD_ASC %>">date (oldest first)</option>
			</select>
		</form>
	</div>
</div>
<div id="mainGroupwindowContent" style="clear: left; margin-top: 0.5em;">

		<%
		int currentPage = 0;
		if(request.getParameter("pageID") != null){
			currentPage = Integer.parseInt(request.getParameter("pageID"));
		}else {
			currentPage = 1;
		}
		
		//start and end of the list-iterator
		int iterStart = (currentPage * 10) - 10;
		int iterEnd = iterStart + 9;
		
		numberofGroups = mygroups.size();
		
		if(numberofGroups == 0){
			%>
			<p>- You have no groups yet -</p>
			
			<div style="text-align:center; margin-top:20px;">
			<a href="<%=VIEW_VOCABULARY.RESTful_CREATE_NEW_GROUP %>" title="Create a new group..." style="font-size: 3em; font-weight: bold;">
				Create a new group!
				<!-- <img src="<%= request.getContextPath() %>/images/create_group_Bt1.png" alt="Create a new group" width="400px" style="text-decoration:none;border:none;"/> -->
			</a>
			</div>
			<%
		}else {
		ListIterator<Group> it = mygroups.listIterator(iterStart);
	//Iterator it = mygroups.iterator(); 
		boolean editGroups = true; %>
	<%@ include file="./groupme_list_groups.jsp" %>
		<%} %>
</div>

<div id="pagenumbers" style="text-align: center; margin-top: 1em; font-size:0.8em;">
	<%
		int resultListSize = numberofGroups;
		if(resultListSize > 10){
			int numberofResultpages = (int) Math.ceil(resultListSize / 10.0);
			
			String pagerequestURL = VIEW_VOCABULARY.RESTful_MyGroupMe_GROUPS + "?pageID=";
			if(currentPage > 1){
				int showPrev = currentPage-1;
				%>
				<span><strong><a href="<%= pagerequestURL + showPrev %>">previous</a></strong></span>&nbsp;&nbsp;
				<%
			}
			out.print("<span>");
			if(numberofResultpages <= 10){
				for(int i = 1; i <= numberofResultpages; i++){
					%>
					<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;color:red;\"");}%>><%=i %></a>
					<%
				}
			}else {
				if(numberofResultpages - currentPage >= 10){
					int start = 0;
					int end = 0;
					
					if(currentPage < 7){
						start = 1;
						end = 7;
					}else {
						
						%>
						<a href="<%= pagerequestURL + 1 %>"><%=1 %></a>
						<%
						out.print("&nbsp;...&nbsp;");
						start = currentPage - 3;
						end = currentPage + 3;
					}
					
					for(int i = start; i <= end; i++){
						%>
						<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;color:red;\"");}%>><%=i %></a>
						<%
					}
					out.print("&nbsp;...&nbsp;");
					%>
					<a href="<%= pagerequestURL + numberofResultpages %>"><%=numberofResultpages %></a>
					<%
				}
				else {
					%>
					<a href="<%= pagerequestURL + 1 %>"><%=1 %></a>
					<%
					out.print("&nbsp;...&nbsp;");
					//out.print("...&nbsp;");
					for(int i = numberofResultpages - 10; i <= numberofResultpages; i++){
						%>
						<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;color:red;\"");}%>><%=i %></a>
						<%
					}
				}
			}
			out.print("</span>");
			if(currentPage < numberofResultpages){
				int showNext = currentPage+1;
				%>
				<span><strong><a href="<%= pagerequestURL + showNext %>">next</a></strong></span>&nbsp;&nbsp;
				<%
			}
		}
	%>
</div>		